#include "snake.h"
#include <iostream>
#include <QWidget>
#include <QKeyEvent>
#include <QPaintEvent>
#include <QPainter>
#include <QPushButton>
Snake::Snake()
{
    //初始化蛇身
    QRectF rect(300,180,nodeWidth,nodeHeight);
    snake.append(rect);
    addTop();
    addTop();
}

void Snake::addTop()
{
    QPointF leftTop;
    QPointF rightBotom;
    leftTop=QPointF(snake[0].x(),snake[0].y()-nodeHeight);
    rightBotom=snake[0].topRight();
    snake.insert(0,QRectF(leftTop,rightBotom));
}

void Snake::addDown()
{
    QPointF leftTop;
    QPointF rightBotom;
    leftTop=snake[0].bottomLeft();
    rightBotom=snake[0].bottomRight()+QPointF(0,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBotom));
}

void Snake::addLeft()
{
    QPointF leftTop;
    QPointF  rightBottom;
    leftTop=snake[0].topLeft()-QPointF(nodeWidth,0);
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));

}
void Snake::addRight()
{
    QPointF leftTop;
    QPointF rightBottom;
    leftTop=snake[0].topRight();
    rightBottom=leftTop+QPointF(nodeWidth,nodeHeight);
    snake.insert(0,QRectF(leftTop,rightBottom));
}
void Snake::deleteLast()
{
    //通过在蛇头添加新物块在蛇尾减去物块来实现移动
    snake.removeLast();
}
